package com.zkh360.service.message.log;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.filter.config.ConfigTools;

import ch.qos.logback.core.db.DriverManagerConnectionSource;

@Configuration
public class LogDriverManagerConnectionSource extends DriverManagerConnectionSource {
	
	private String publicKey;
	
	@Override
	public Connection getConnection() throws SQLException {
		Connection connection = null;
		if (getUser() == null) {
			connection = DriverManager.getConnection(getUrl());
		} else {
			try {
				connection = DriverManager.getConnection(getUrl(), getUser(), ConfigTools.decrypt(publicKey, getPassword()));
			} catch (Exception e) {
				throw new SQLException("druid 解密异常");
			}
		}
		
		return connection;
	}

	public String getPublicKey() {
		return publicKey;
	}

	public void setPublicKey(String publicKey) {
		this.publicKey = publicKey;
	}
	
	public static void main(String[] args) throws Exception {
		System.out.println(ConfigTools.decrypt("MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAI9dITnaMVIX7HVdHSyrP+jz7pZwEVW1TJpNXz0UeescTM1O/dtQUFH1/DhjIVP5tQXLYmVcctshrk4YEW++nCUCAwEAAQ==", "L2t8tt/2kPvnIe/RzN9QzgA7RgVVJ4a0lNFu1j3XZyyFPMlnlAMFNQxr7hCl3h3GaCl9cZRtkpxZnka0gXAgOQ=="));
	}
}
